package com.sofwin.controller;

import com.github.pagehelper.PageInfo;
import com.sofwin.Constants;
import com.sofwin.annotation.Log;
import com.sofwin.dto.UserDto;
import com.sofwin.entity.AjaxResult;
import com.sofwin.pojo.SysDept;
import com.sofwin.pojo.SysRole;
import com.sofwin.pojo.SysUser;
import com.sofwin.service.DeptService;
import com.sofwin.service.RoleService;
import com.sofwin.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apiguardian.api.API;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * ClassName: UserController
 * Description: TODO
 * date: 2022/4/26 3:39 下午
 *
 * @author andyliu
 * @qq 115176513
 * @since JDK 1.8
 */
@Controller
@Api(tags = "用户管理",produces = "application/json")
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    @Autowired
    private DeptService deptService;

    @GetMapping()
    // 这个请求不会出现在文档中
    @ApiIgnore
    public String toData(){
        return "user/data";
    }
    @GetMapping("/toEdit")
    @ApiIgnore
    public String toEidt(Integer id, ModelMap map){
        List<SysDept> depts = deptService.queryDepts(null);
        map.put("depts",depts);
        List<SysRole> roles = roleService.queryAllRoles();
        map.put("roles",roles);
        if(id!=null){
            SysUser user = userService.queryById(id);
            map.put("user",user);
        }
        return "user/edit";
    }

    /**
     * 分页查询请求
     * @param user
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @PostMapping("/data")
    @ResponseBody
    @ApiOperation(httpMethod = "POST",produces = "application/json",value="用户分页查询数据")
    @Log(module = "用户管理",operatorType = 2)
    public AjaxResult data(SysUser user,@RequestParam(required = true,defaultValue = "1") Integer pageNumber, @RequestParam(required = true,defaultValue= Constants.PAGE_SIZE) Integer pageSize){
        PageInfo<UserDto> pageInfo = userService.queryUsers(user, pageNumber, pageSize);

        return AjaxResult.success(pageInfo);
    }

    /**
     * 根据id获取用户信息
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    @ResponseBody
    @ApiOperation(value = "根据id获取用户信息",httpMethod = "GET",produces = "application/json")
    public AjaxResult info(@PathVariable Integer id){
        SysUser user = userService.queryById(id);
        return AjaxResult.success(user);
    }

    /**
     * 新增或更新请求
     * @param user
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    @ApiOperation(value = "保存或更新用户信息",httpMethod = "POST",produces = "application/json")
    @Log(module = "用户管理",operatorType = 3)
    public AjaxResult save(SysUser user){
        if(user.getStatus()==null){
            user.setStatus(1);
        }
        boolean status = userService.saveOrUpdate(user);
        if(status){
            return AjaxResult.success(status);
        }
        return AjaxResult.error(status);
    }

    /**
     * 根据id删除用户信息
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    @ApiOperation(value="根据id删除用户信息",httpMethod = "DELETE",produces = "application/json")
    @Log(module = "用户管理",operatorType = 4)
    public AjaxResult delete(@PathVariable Integer id){
        boolean status = userService.removeById(id);
        if(status){
            return AjaxResult.success(status);
        }
        return AjaxResult.error(status);
    }
    @DeleteMapping("/deletes")
    @ResponseBody
    @ApiOperation(value ="批量删除用户信息", httpMethod = "DELETE",produces = "application/json")
    @Log(module = "用户管理",operatorType = 4)
    public AjaxResult deletes(@RequestParam(name="ids[]") Integer[] ids){
        boolean status = userService.removeByIds(ids);if(status){
            return AjaxResult.success(status);
        }
        return AjaxResult.error(status);

    }
}
